/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.constant.IsfdMeshModelConstants;
import com.comac.ins.isfd.domain.bo.IsfdMeshQueryBo;
import com.comac.ins.isfd.service.IIsfdMeshOp2Service;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 自然网格结果库
 *
 * @author hxloongs
 * @date 2025-03-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/mesh/op2")
public class IsfdMeshOp2Controller extends BaseController {

    @Autowired
    private IIsfdMeshOp2Service iIsfdMeshOp2Service;

    /**
     * 自然网格结果库-分页查询
     */
    @SaCheckPermission("isfd:mesh:list")
    @PostMapping("/list")
    public TableDataInfo<Document> queryTaskList(@Validated @RequestBody IsfdMeshQueryBo query) {
        // 校验 fileName 是否包含非法字符
        String fileName = query.getFileName();
        if (StringUtils.isNotEmpty(fileName)) {
            for (char illegalChar : IsfdMeshModelConstants.ILLEGAL_CHARS.toCharArray()) {
                if (fileName.contains(String.valueOf(illegalChar))) {
                    throw new BaseException("文件名暂时不支持字符: " + illegalChar + "的检索");
                }
            }
        }
        return iIsfdMeshOp2Service.queryList(query);
    }

    /**
     * 自然网格结果库-文件下载
     */
    @SaCheckPermission("isfd:mesh:download")
    @RepeatSubmit()
    @GetMapping("/file/download")
    public void download(@RequestParam("_id") String id, HttpServletResponse response) {
        iIsfdMeshOp2Service.download(id, response);
    }
}
